<HTML><HEAD>
<!--
    ------
    STACKS
    ------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

    // Create an Array
    function createArray(n)
    {
        for (var i = 0; i < n; i++) {this[i] = 0}
        this.length = n;
        return this
    }
    
    // Initialize Global Variables
    stack = createArray(50) // 50 should be big enough
    sktop = 0               // Top of the Stack

    // Show the Stack
    function showstack()
    {
        var answer=""
        for (var i = 0; i < sktop; i++) answer = stack[i] + ": " + answer
        document.forms[0].stack.value = answer
        document.forms[0].input.focus();document.forms[0].input.select()
    }
    
    // Push value onto stack
    function push()
    {
        stack[sktop]=""+document.forms[0].input.value
        sktop++ // increase top
        showstack()
    }
    
    // Pop value off of stack
    function pop()
    {
        if (sktop > 0) 
        {
            document.forms[0].input.value=""+stack[sktop-1]
            sktop -= 1 // decrease top
        }
        else alert("Stack is empty!")
        showstack()
    }

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onLoad="document.forms[0].input.focus();document.forms[0].input.select()">

<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
    ALIGN = CENTER>Stacks</H1></FONT>

    <FONT COLOR="770000">
        Stacks are easy to implement in JavaScript--use
        an array with a "top" pointer. To try this stack
        applet out, just start typing.    Press [Push] to store each value
        onto the stack.
    </FONT></BLOCKQUOTE>

    <BR><BR>

    </SCRIPT>

    <CENTER><FORM><TABLE BORDER=1>
    
    <TR>
    <TD align=center  colspan=2><input type="text" name="stack" value="" 
    size=40>: STACK</TD>
    </TR>
    
    <TR>
    <TD align=center  colspan=2><input type="text" name="input" 
        value="" size=8></TD>
    </TR>
    
    <TR>
    <TD align=center>
    <input type="button" value="Push" onClick="push()"></TD>
    <TD align=center>
    <input type="button" value="Pop" onClick="pop()"></TD>
    </TR> 
    
    </TABLE></FORM></CENTER>

    <BR><BR>

    <FONT COLOR="007777"><H2>Discussion</H2></FONT>
    <FONT SIZE=4>
    Stacks provide a good example of an array application;
    they are easy to implement using JavaScript primitives.
    As an exercise for the reader,
    inspect the following code and figure out where the "popped"
    value goes.
    </FONT>
<FONT COLOR="770000"><PRE>
// Show the Stack
function showstack()
{
    var answer=""
    for (var i = 0; i < sktop; i++) answer = stack[i] + ": " + answer
    document.forms[0].stack.value = answer
    document.forms[0].input.focus();document.forms[0].input.select()
}

// Push value onto stack
function push()
{
    stack[sktop]=""+document.forms[0].input.value
    sktop++ // increase top
    showstack()
}

// Pop value off of stack
function pop()
{
    if (sktop > 0) 
    {
        document.forms[0].input.value=""+stack[sktop-1]
        sktop -= 1 // decrease top
    }
    else alert("Stack is empty!")
    showstack()
}
</PRE></FONT>


<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>